var expect = require('chai').expect; // chai librarysinin expect ozelliklerini kullan
//var cytoscape = require('../js/cytoscape.js', cytoscape); 

describe('Utility scripts (sorting, geometry, etc)', function () {

  before(function () {
    // runs before all tests in this block
  });
  after(function () {
    // runs after all tests in this block
  });
  beforeEach(function () {
    // runs before each test in this block
  });
  afterEach(function () {
    // runs after each test in this block
  });

  it('DimensionD.js', function () {
    var dim = new DimensionD(5, 10);
    expect([dim.getWidth(), dim.getHeight()]).to.deep.equal([5, 10]);

    dim.setWidth(50);
    dim.setHeight(100);
    expect([dim.getWidth(), dim.getHeight()]).to.deep.equal([50, 100]);
  });

  it('RectangleD.js', function () {
    var rectD = new RectangleD();
    rectD.setX(10);
    rectD.setY(20);
    rectD.setHeight(15);
    rectD.setWidth(25);

    expect([rectD.getWidth(), rectD.getHeight(), rectD.getX(), rectD.getY()]).to.deep.equal([25, 15, 10, 20]);

    rectD = new RectangleD(5, 10, 15, 20);
    expect([rectD.getWidth(), rectD.getHeight(), rectD.getX(), rectD.getY()]).to.deep.equal([15, 20, 5, 10]);

    var rectA = new RectangleD(0, 0, 100, 200);
    var rectB = new RectangleD(50, 50, 25, 100);
    expect([rectA.getBottom(), rectA.getCenterX(), rectA.getCenterY(),
      rectA.getRight(), rectA.getHeightHalf(), rectA.getWidthHalf()]).to.deep.equal([200, 50, 100, 100, 100, 50]);
    expect(rectA.intersects(rectB)).to.be.ok;

    rectB.setX(110);
    rectB.setY(210);
    expect(rectA.intersects(rectB)).to.not.be.ok;
  });

  it('IGeometry.js getIntersection', function () {
    var rectA = new RectangleD(5, 6, 2, 4);
    var rectB;
    var clipPoints = new Array(4);

    rectB = new RectangleD(0, 4, 1, 4);
    IGeometry.getIntersection(rectA, rectB, clipPoints);
    expect(clipPoints).to.deep.equal([5, 7.636363636363637, 1, 6.181818181818182]);

    rectB = new RectangleD(1, 4, 1, 2);
    clipPoints = new Array(4);
    IGeometry.getIntersection(rectA, rectB, clipPoints);
    expect(clipPoints).to.deep.equal([5, 7.333333333333333, 2, 5.333333333333333]);

    rectB = new RectangleD(1, 3, 3, 2);
    clipPoints = new Array(4);
    IGeometry.getIntersection(rectA, rectB, clipPoints);
    expect(clipPoints).to.deep.equal([5, 6.857142857142858, 3.375, 5]);

    rectB = new RectangleD(2, 3, 2, 4);
    clipPoints = new Array(4);
    IGeometry.getIntersection(rectA, rectB, clipPoints);
    expect(clipPoints).to.deep.equal([5, 7, 4, 6]);

    rectB = new RectangleD(3, 3, 2, 2);
    clipPoints = new Array(4);
    IGeometry.getIntersection(rectA, rectB, clipPoints);
    expect(clipPoints).to.deep.equal([5, 6, 4.5, 5]);

    rectB = new RectangleD(3, 2, 4, 2);
    clipPoints = new Array(4);
    IGeometry.getIntersection(rectA, rectB, clipPoints);
    expect(clipPoints).to.deep.equal([5.6, 6, 5.2, 4]);

    rectB = new RectangleD(6, 3, 2, 2);
    clipPoints = new Array(4);
    IGeometry.getIntersection(rectA, rectB, clipPoints);
    expect(clipPoints).to.deep.equal([6.5, 6, 6.75, 5]);

    rectB = new RectangleD(9, 2, 4, 2);
    clipPoints = new Array(4);
    IGeometry.getIntersection(rectA, rectB, clipPoints);
    expect(clipPoints).to.deep.equal([7, 7, 10, 4]);

    rectB = new RectangleD(9, 3, 2, 2);
    clipPoints = new Array(4);
    IGeometry.getIntersection(rectA, rectB, clipPoints);
    expect(clipPoints).to.deep.equal([7, 7, 9, 5]);

    rectB = new RectangleD(8, 3, 2, 4);
    clipPoints = new Array(4);
    IGeometry.getIntersection(rectA, rectB, clipPoints);
    expect(clipPoints).to.deep.equal([7, 7, 8, 6]);

    rectB = new RectangleD(11, 3, 3, 2);
    clipPoints = new Array(4);
    IGeometry.getIntersection(rectA, rectB, clipPoints);
    expect(clipPoints).to.deep.equal([7, 7.384615384615385, 11, 4.923076923076923]);

    rectB = new RectangleD(11, 4, 1, 2);
    clipPoints = new Array(4);
    IGeometry.getIntersection(rectA, rectB, clipPoints);
    expect(clipPoints).to.deep.equal([7, 7.454545454545455, 11, 5.2727272727272725]);

    rectB = new RectangleD(10, 4, 1, 4);
    clipPoints = new Array(4);
    IGeometry.getIntersection(rectA, rectB, clipPoints);
    expect(clipPoints[0]).to.equal(7);
    expect(clipPoints[1]).to.equal(7.555555555555555);
    expect(clipPoints[2]).to.equal(10);
    expect(clipPoints[3]).to.equal(6.222222222222222);

    rectB = new RectangleD(10, 5, 2, 2);
    clipPoints = new Array(4);
    IGeometry.getIntersection(rectA, rectB, clipPoints);
    expect(clipPoints[0]).to.equal(7);
    expect(clipPoints[1]).to.equal(7.6);
    expect(clipPoints[2]).to.equal(10);
    expect(clipPoints[3]).to.equal(6.4);

    rectB = new RectangleD(9, 4.5, 2, 4);
    clipPoints = new Array(4);
    IGeometry.getIntersection(rectA, rectB, clipPoints);
    expect(clipPoints[0]).to.equal(7);
    expect(clipPoints[1]).to.equal(7.625);
    expect(clipPoints[2]).to.equal(9);
    expect(clipPoints[3]).to.equal(6.875);

    rectB = new RectangleD(10, 5.8, 0.4, 2);
    clipPoints = new Array(4);
    IGeometry.getIntersection(rectA, rectB, clipPoints);
    expect(clipPoints[0]).to.equal(7);
    expect(clipPoints[1]).to.equal(7.714285714285714);
    expect(clipPoints[2]).to.equal(10);
    expect(clipPoints[3]).to.equal(6.857142857142857);

    rectB = new RectangleD(11, 6, 2, 2);
    clipPoints = new Array(4);
    IGeometry.getIntersection(rectA, rectB, clipPoints);
    expect(clipPoints[0]).to.equal(7);
    expect(clipPoints[1]).to.equal(7.833333333333333);
    expect(clipPoints[2]).to.equal(11);
    expect(clipPoints[3]).to.equal(7.166666666666667);

    rectB = new RectangleD(10, 7.8, 0.4, 2);
    clipPoints = new Array(4);
    IGeometry.getIntersection(rectA, rectB, clipPoints);
    expect(clipPoints[0]).to.equal(7);
    expect(clipPoints[1]).to.equal(8.19047619047619);
    expect(clipPoints[2]).to.equal(10);
    expect(clipPoints[3]).to.equal(8.761904761904763);

    rectB = new RectangleD(9, 7.5, 1, 4);
    clipPoints = new Array(4);
    IGeometry.getIntersection(rectA, rectB, clipPoints);
    expect(clipPoints[0]).to.equal(7);
    expect(clipPoints[1]).to.equal(8.428571428571429);
    expect(clipPoints[2]).to.equal(9);
    expect(clipPoints[3]).to.equal(9.285714285714286);

    rectB = new RectangleD(10, 7, 2, 2);
    clipPoints = new Array(4);
    IGeometry.getIntersection(rectA, rectB, clipPoints);
    expect(clipPoints[0]).to.equal(7);
    expect(clipPoints[1]).to.equal(8);
    expect(clipPoints[2]).to.equal(10);
    expect(clipPoints[3]).to.equal(8);

    rectB = new RectangleD(10, 9, 2, 6);
    clipPoints = new Array(4);
    IGeometry.getIntersection(rectA, rectB, clipPoints);
    expect(clipPoints[0]).to.equal(7);
    expect(clipPoints[1]).to.equal(8.8);
    expect(clipPoints[2]).to.equal(10);
    expect(clipPoints[3]).to.equal(11.2);

    rectB = new RectangleD(11, 9, 2, 4);
    clipPoints = new Array(4);
    IGeometry.getIntersection(rectA, rectB, clipPoints);
    expect(clipPoints[0]).to.equal(7);
    expect(clipPoints[1]).to.equal(8.5);
    expect(clipPoints[2]).to.equal(11);
    expect(clipPoints[3]).to.equal(10.5);

    rectB = new RectangleD(12, 8, 4, 2);
    clipPoints = new Array(4);
    IGeometry.getIntersection(rectA, rectB, clipPoints);
    expect(clipPoints[0]).to.equal(7);
    expect(clipPoints[1]).to.equal(8.125);
    expect(clipPoints[2]).to.equal(12);
    expect(clipPoints[3]).to.equal(8.75);

    rectB = new RectangleD(7, 9, 2, 4);
    clipPoints = new Array(4);
    IGeometry.getIntersection(rectA, rectB, clipPoints);
    expect(clipPoints[0]).to.equal(6);
    expect(clipPoints[1]).to.equal(8);
    expect(clipPoints[2]).to.equal(8);
    expect(clipPoints[3]).to.equal(11);

    rectB = new RectangleD(8, 9, 4, 2);
    clipPoints = new Array(4);
    IGeometry.getIntersection(rectA, rectB, clipPoints);
    expect(clipPoints[0]).to.equal(7);
    expect(clipPoints[1]).to.equal(8.5);
    expect(clipPoints[2]).to.equal(8);
    expect(clipPoints[3]).to.equal(9);

    rectB = new RectangleD(10, 9, 2, 2);
    clipPoints = new Array(4);
    IGeometry.getIntersection(rectA, rectB, clipPoints);
    expect(clipPoints[0]).to.equal(7);
    expect(clipPoints[1]).to.equal(8.4);
    expect(clipPoints[2]).to.equal(10);
    expect(clipPoints[3]).to.equal(9.6);

    rectB = new RectangleD(6, 10, 2, 2);
    clipPoints = new Array(4);
    IGeometry.getIntersection(rectA, rectB, clipPoints);
    expect(clipPoints[0]).to.equal(6);
    expect(clipPoints[1]).to.equal(8);
    expect(clipPoints[2]).to.equal(7);
    expect(clipPoints[3]).to.equal(11);

    rectB = new RectangleD(3, 8, 4, 2);
    clipPoints = new Array(4);
    IGeometry.getIntersection(rectA, rectB, clipPoints);
    expect(clipPoints[0]).to.equal(6);
    expect(clipPoints[1]).to.equal(8);
    expect(clipPoints[2]).to.equal(5);
    expect(clipPoints[3]).to.equal(9);

    rectB = new RectangleD(3, 9, 2, 2);
    clipPoints = new Array(4);
    IGeometry.getIntersection(rectA, rectB, clipPoints);
    expect(clipPoints[0]).to.equal(6);
    expect(clipPoints[1]).to.equal(8);
    expect(clipPoints[2]).to.equal(4);
    expect(clipPoints[3]).to.equal(10);

    rectB = new RectangleD(2, 8, 4, 4);
    clipPoints = new Array(4);
    IGeometry.getIntersection(rectA, rectB, clipPoints);
    expect(clipPoints[0]).to.equal(6);
    expect(clipPoints[1]).to.equal(8);
    expect(clipPoints[2]).to.equal(4);
    expect(clipPoints[3]).to.equal(10);

    rectB = new RectangleD(2, 8, 2, 2);
    clipPoints = new Array(4);
    IGeometry.getIntersection(rectA, rectB, clipPoints);
    expect(clipPoints[0]).to.equal(5);
    expect(clipPoints[1]).to.equal(8.333333333333334);
    expect(clipPoints[2]).to.equal(4);
    expect(clipPoints[3]).to.equal(8.666666666666666);

    rectB = new RectangleD(1, 8, 2, 4);
    clipPoints = new Array(4);
    IGeometry.getIntersection(rectA, rectB, clipPoints);
    expect(clipPoints[0]).to.equal(5);
    expect(clipPoints[1]).to.equal(8.5);
    expect(clipPoints[2]).to.equal(3);
    expect(clipPoints[3]).to.equal(9.5);

    rectB = new RectangleD(1, 8.5, 1, 4);
    clipPoints = new Array(4);
    IGeometry.getIntersection(rectA, rectB, clipPoints);
    expect(clipPoints[0]).to.equal(5);
    expect(clipPoints[1]).to.equal(8.555555555555555);
    expect(clipPoints[2]).to.equal(2);
    expect(clipPoints[3]).to.equal(10.222222222222221);

    rectB = new RectangleD(3, 7, 2, 2);
    clipPoints = new Array(4);
    IGeometry.getIntersection(rectA, rectB, clipPoints);
    expect(clipPoints[0]).to.equal(6);
    expect(clipPoints[1]).to.equal(8);
    expect(clipPoints[2]).to.equal(4);
    expect(clipPoints[3]).to.equal(8);

    rectB = new RectangleD(1, 7.5, 1, 4);
    clipPoints = new Array(4);
    IGeometry.getIntersection(rectA, rectB, clipPoints);
    expect(clipPoints[0]).to.equal(5);
    expect(clipPoints[1]).to.equal(8.333333333333334);
    expect(clipPoints[2]).to.equal(2);
    expect(clipPoints[3]).to.equal(9.333333333333334);

    rectB = new RectangleD(3, 7.8, 0.4, 2);
    clipPoints = new Array(4);
    IGeometry.getIntersection(rectA, rectB, clipPoints);
    expect(clipPoints[0]).to.equal(5);
    expect(clipPoints[1]).to.equal(8.285714285714286);
    expect(clipPoints[2]).to.equal(3.4);
    expect(clipPoints[3]).to.equal(8.742857142857144);

    rectB = new RectangleD(1, 6, 2, 2);
    clipPoints = new Array(4);
    IGeometry.getIntersection(rectA, rectB, clipPoints);
    expect(clipPoints[0]).to.equal(5);
    expect(clipPoints[1]).to.equal(7.75);
    expect(clipPoints[2]).to.equal(3);
    expect(clipPoints[3]).to.equal(7.25);

    rectB = new RectangleD(3, 5.8, 0.4, 2);
    clipPoints = new Array(4);
    IGeometry.getIntersection(rectA, rectB, clipPoints);
    expect(clipPoints[0]).to.equal(5);
    expect(clipPoints[1]).to.equal(7.571428571428571);
    expect(clipPoints[2]).to.equal(3.4);
    expect(clipPoints[3]).to.equal(6.885714285714285);

    rectB = new RectangleD(1, 5, 1, 3);
    clipPoints = new Array(4);
    IGeometry.getIntersection(rectA, rectB, clipPoints);
    expect(clipPoints[0]).to.equal(5);
    expect(clipPoints[1]).to.equal(7.666666666666667);
    expect(clipPoints[2]).to.equal(2);
    expect(clipPoints[3]).to.equal(6.666666666666667);

    rectB = new RectangleD(1, 4, 3, 3);
    clipPoints = new Array(4);
    IGeometry.getIntersection(rectA, rectB, clipPoints);
    expect(clipPoints[0]).to.equal(5);
    expect(clipPoints[1]).to.equal(7.285714285714286);
    expect(clipPoints[2]).to.equal(4);
    expect(clipPoints[3]).to.equal(6.571428571428571);

    var p1 = new Point(10, 20);
    var p2 = new Point(15, 25);
    var p3 = new Point(0, 50);
    var p4 = new Point(12, 30);
    var res = IGeometry.getIntersection(p1, p2, p3, p4);
    expect(res.getX()).to.equal(15.0);
    expect(res.getY()).to.equal(25.0);
  });

  it('IGeometry.js decideDirectionsForOverlappingNodes', function () {
    var rectA = new RectangleD(0, 0, 5, 10);
    var rectB = new RectangleD(5, 0, 10, 5);
    var directions = new Array(2);
    IGeometry.decideDirectionsForOverlappingNodes(rectA, rectB, directions);
    expect(directions).to.deep.equal([-1, 1]);
  });

  it('IGeometry.js calcSeperationAmount', function () {
    var rectA = new RectangleD(0, 0, 100, 100);
    var rectB = new RectangleD(50, 50, 10, 100);
    var overlapAmount = new Array(2);
    IGeometry.calcSeparationAmount(rectA, rectB, overlapAmount, 50);
    expect(overlapAmount).to.deep.equal([52.5, 75]);

    rectB = new RectangleD(0, 0, 100, 100);
    rectA = new RectangleD(50, 50, 10, 100);
    overlapAmount = new Array(2);
    IGeometry.calcSeparationAmount(rectA, rectB, overlapAmount, 50);
    expect(overlapAmount).to.deep.equal([-52.5, -75]);

    rectA = new RectangleD(50, 0, 100, 200);
    rectB = new RectangleD(0, 50, 100, 50);
    overlapAmount = new Array(2);
    IGeometry.calcSeparationAmount(rectA, rectB, overlapAmount, 50);
    expect(overlapAmount).to.deep.equal([-75, -62.5]);

    rectB = new RectangleD(50, 0, 100, 200);
    rectA = new RectangleD(0, 50, 100, 50);
    overlapAmount = new Array(2);
    IGeometry.calcSeparationAmount(rectA, rectB, overlapAmount, 50);
    expect(overlapAmount).to.deep.equal([75, 62.5]);
  });

  it('IGeometry.js getCardinalDirection', function () {
    var res = [IGeometry.getCardinalDirection(5, 4, 3), IGeometry.getCardinalDirection(4, 5, 3)];
    expect(res).to.deep.equal([3, 4]);
  });

  it('IGeometry.js angleOfVector', function () {
    var res = [IGeometry.angleOfVector(5, 1, 4, 2), IGeometry.angleOfVector(4, 2, 5, 1), IGeometry.angleOfVector(4, 2, 4, 1), IGeometry.angleOfVector(4, 1, 4, 2)];
    expect(res).to.deep.equal([2.356194490192345, 5.497787143782138, 4.71238898038469, 1.5707963267948966]);
  });

  it('IGeometry.js radian2degree', function () {
    var res = [IGeometry.radian2degree(IGeometry.TWO_PI), IGeometry.radian2degree(IGeometry.THREE_PI)];
    expect(res).to.deep.equal([360, 540]);
  });

  it('IGeometry.js doIntersect', function () {
    var p1 = new PointD(10, 20);
    var p2 = new PointD(15, 25);
    var p3 = new PointD(0, 50);
    var p4 = new PointD(12, 30);

    var res = IGeometry.doIntersect(p1, p2, p3, p4);
    expect(res).to.not.be.ok;

    p1 = new PointD(0, 0);
    p2 = new PointD(100, 100);
    p3 = new PointD(0, 100);
    p4 = new PointD(100, 0);

    res = IGeometry.doIntersect(p1, p2, p3, p4);
    expect(res).to.be.ok;

    p3 = new PointD(200, 200);
    p4 = new PointD(300, 300);

    res = IGeometry.doIntersect(p1, p2, p3, p4);
    expect(res).to.not.be.ok;
  });

  it('Point.js', function () {
    var p = new Point();

    expect(p.getX()).to.equal(0);
    expect(p.getY()).to.equal(0);

    p = new Point(5, 10);

    expect(p.getX()).to.equal(5);
    expect(p.getY()).to.equal(10);

    var p2 = new Point(p);
    expect(p2.getX()).to.equal(5);
    expect(p2.getY()).to.equal(10);

    expect(p.equals(p)).to.be.ok;
    expect(p.equals(p2)).to.be.ok;

    p.translate(5, -1);
    expect(p.getX()).to.equal(10);
    expect(p.getY()).to.equal(9);

    p.setLocation(5, 3);
    expect(p.getX()).to.equal(5);
    expect(p.getY()).to.equal(3);

    p.setLocation(new Point(7, 8));
    expect(p.getX()).to.equal(7);
    expect(p.getY()).to.equal(8);

    p.setLocation(5.3, 4.3);
    expect(p.getX()).to.equal(Math.floor(5.3 + 0.5));
    expect(p.getY()).to.equal(Math.floor(4.3 + 0.5));

    p.setLocation(10, 20);
    expect(p.toString()).to.equal("Point[x=" + p.getX() + ",y=" + p.getY() + "]");
  });

  it('PointD.js', function () {
    var p = new PointD();

    expect([p.getX(), p.getY()]).to.deep.equal([0, 0]);

    p = new PointD(5, 10);

    expect([p.getX(), p.getY()]).to.deep.equal([5, 10]);

    var p2 = p.getCopy();
    p2.setX(3);
    p2.setY(7);

    expect(p.getDifference(p2)).to.deep.equal(new DimensionD(2, 3));

    p.translate(new DimensionD(5, -1));
    expect([p.getX(), p.getY()]).to.deep.equal([10, 9]);
  });

  it('Transform.js', function () {
    var trans = new Transform();

    trans.setWorldOrgX(0.0);
    trans.setWorldOrgY(0.0);
    trans.setWorldExtX(100.0);
    trans.setWorldExtY(50.0);

    trans.setDeviceOrgX(10.0);
    trans.setDeviceOrgY(20.0);
    trans.setDeviceExtX(50.0);
    trans.setDeviceExtY(-100.0);

    var rectWorld = new RectangleD(12.0, -25.0, 150.0, 150.0);
    var pointWorld = new PointD(rectWorld.x, rectWorld.y);
    var dimWorld = new DimensionD(rectWorld.width, rectWorld.height);

    var pointDevice = trans.transformPoint(pointWorld);
    var dimDevice = trans.transformDimension(dimWorld);
    var rectDevice = trans.transformRect(rectWorld);

    expect([pointDevice.getX(), pointDevice.getY()]).to.deep.equal([16, 70]);
    expect([dimDevice.getWidth(), dimDevice.getHeight()]).to.deep.equal([75, -300]);
    expect([rectDevice.getX(), rectDevice.getY(), rectDevice.getWidth(), rectDevice.getHeight()]).to.deep.equal([16, 70, 75, -300]);
  });

  it('QuickSort.js', function () {
    var array = [62, 47, 27, 33, 55, 89, 27];
    var qs = new QuickSort(array);
    qs.quicksort();
    expect(array).to.deep.equal([89, 62, 55, 47, 33, 27, 27]);
  });

  it('HashSet.js', function () {
    function Deneme(a, b) {
      this.a = a;
      this.b = b;
    }

    var d1 = new Deneme(5, 3);
    var d2 = new Deneme(5, 3);

    var hs = new HashSet();
    hs.add(d1);
    expect(hs.size()).to.equal(1);
    hs.add(d2);
    expect(hs.size()).to.equal(1);
    hs.add(new Deneme(4, 2));
    expect(hs.size()).to.equal(2);
    hs.remove(d1);
    expect(hs.size()).to.equal(1);

    var list = [];
    hs.addAllTo(list);

    expect(list.length).to.equal(1);

    hs.addAllTo(list);
    expect(list.length).to.equal(2);
    
    hs = new HashSet();
    var list2 = [];
    list2.push(new Deneme(5, 3));
    list2.push(new Deneme(5, 3));
    list2.push(new Deneme(4, 2));
    
    hs.addAll(list2);
    expect(hs.size()).to.equal(2);
  });

  it('RandomSeed.js', function () {
    var array1 = [];
    var array2 = [];

    var random1 = new RandomSeed(34);
    var random2 = new RandomSeed(34);

    for (var i = 0; i < 100; i++) {
      array1[i] = random1.next();
      array2[i] = random2.next();
    }

    expect(array1).to.deep.equal(array2);

  });
});